package com.youlian.utils;

import java.io.File;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import android.app.Application;
import android.graphics.Bitmap;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKGeneralListener;
import com.youlian.common.Config;
import com.youlian.manager.model.CardsResult;

public class YoulianApplication extends Application {

	
	public static File cacheDir;
	public static YoulianApplication instance = null;

	/** 品牌数据集(特卖会) **/
	public Map<Integer,ArrayList<CardsResult>> brandMap = new ConcurrentHashMap<Integer, ArrayList<CardsResult>>();

	/** 列表图片数据源(适配器公用数据源) **/
	public Map<String,SoftReference<Bitmap>> mBitmapMap = new ConcurrentHashMap<String, SoftReference<Bitmap>>();

	/** 特卖会列表图片数据源(适配器公用数据源) **/
	public Map<String, SoftReference<Bitmap>> brandBitmapMap = Collections.synchronizedMap(new LinkedHashMap<String, SoftReference<Bitmap>>());

	
	
	//百度MapAPI的管理类
	public BMapManager mBMapMan = null;
	
	// 授权Key
	// TODO: 请输入您的Key,
	// 申请地址：http://dev.baidu.com/wiki/static/imap/key/
	public String mStrKey = "B7AE5B0D790891D510BA79BE15681A505B2C6441";
	public boolean m_bKeyRight = true;	// 授权Key正确，验证通过
	
	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
	public static class MyGeneralListener implements MKGeneralListener {
		@Override
		public void onGetNetworkState(int iError) {
			Toast.makeText(YoulianApplication.instance.getApplicationContext(), "您的网络出错啦！",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onGetPermissionState(int iError) {
			if (iError ==  MKEvent.ERROR_PERMISSION_DENIED) {
				// 授权Key错误：
				Toast.makeText(YoulianApplication.instance.getApplicationContext(), 
						"请在BMapApiDemoApp.java文件输入正确的授权Key！",
						Toast.LENGTH_LONG).show();
				YoulianApplication.instance.m_bKeyRight = false;
			}
		}
		
	}
	
	@Override
    public void onCreate() {
		instance = this;
		mBMapMan = new BMapManager(this);
		mBMapMan.init(this.mStrKey, new MyGeneralListener());
		super.onCreate();
	}
	@Override
	//建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
	public void onTerminate() {
		// TODO Auto-generated method stub
		if (mBMapMan != null) {
			mBMapMan.destroy();
			mBMapMan = null;
		}
		super.onTerminate();
	}
	
	public void initFloat(){
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)){
			String SD_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
            cacheDir = new File(SD_PATH,Config.imagesPath);
        }else{
            cacheDir = getCacheDir();
            //File file = new File(getFilesDir().getPath());// 方法用于获取/data/data/cache目录
        }
        if (!cacheDir.exists()){
            cacheDir.mkdirs();
        }
        System.out.println("cacheDir:"+cacheDir.getAbsolutePath());
	}

	//单例模式中获取唯一的MyApplication实例
	public synchronized static YoulianApplication getInstance() {
		synchronized (instance) {
			if (null == instance) {
				synchronized (instance) {
					System.out.println("===================BaseApplication getInstance=======================");
					instance = new YoulianApplication();
				}
			}
		}
		return instance;
	}
}
